
package edu.whu.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.whu.demo.domain.Comment;
import edu.whu.demo.domain.CommentDTO;
import edu.whu.demo.domain.User;
import edu.whu.demo.exception.CommentException;
import edu.whu.demo.exception.MessageException;
import edu.whu.demo.service.impl.AuditService;
import edu.whu.demo.service.impl.CommentServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-11-24
 */

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentServiceImpl commentService;

    @Autowired
    AuditService auditService;
    //显示评论

    @ApiOperation("获得评论，根据活动id获得评论")
    @GetMapping("/getComment/{activityId}")
    public List<CommentDTO> getComment(@PathVariable Long activityId){
        return commentService.findComment2(activityId);
    }
    //需要前端提供活动id？


    @ApiOperation("查找评论")
    @GetMapping("")
    @PreAuthorize("hasRole('admin')")
    public IPage<CommentDTO> searchComment(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(required=false) String keyword,
                                        @RequestParam(required=false) Boolean order){
        Map<String,Object> condition = new HashMap<>();
        if(keyword != null){
            condition.put("keyword",keyword);
        }
        if(order != null){
            condition.put("order",order);
        }
        return commentService.searchComment(condition,pageNum,pageSize);
    }

    //添加评论
    @ApiOperation("添加评论，前端封装好Comment")
    @PostMapping("")
    @PreAuthorize("hasRole('normal')")
    public Comment addComment(@RequestBody Comment comment) throws CommentException, MessageException {
        System.out.println(comment);
        if(!auditService.textAudit(comment.getContent())){
            System.out.println("评论审核不通过");
            comment.setNeedCheck(true);
        }
        return commentService.addComment(comment);
    }
    //需要前端提供封装好的comment，前端已知目标对象是活动还是评论，点赞数默认0.

    //删除评论--可以删除自己的评论
    @ApiOperation("删除评论")
    @DeleteMapping("/{id}")
    public void deleteComment(@PathVariable Long id) throws CommentException {
         commentService.deleteComment(id);
    }
    //需要前端提供评论的id


    //点赞评论
    @ApiOperation("点赞评论")
    @PutMapping("/like/{id}")
    @PreAuthorize("hasRole('normal')")
    public void likeComment(@PathVariable Long id){
        commentService.likeComment(id);
    }//需要前端提供评论的id

    @ApiOperation("取消点赞评论")
    @PutMapping("/disLike/{id}")
    @PreAuthorize("hasRole('normal')")
    public void disLikeComment(@PathVariable Long id){
        commentService.disLikeComment(id);
    }
  /*  //修改评论--仅提供接口
    @ApiOperation("修改评论")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('normal')")
    public void updateComment(@PathVariable Long id,@RequestPart Comment comment){
        commentService.updateComment(id,comment);
    }*/

}

